survey<-read.csv("Survey_full_responses.csv", skipNul=TRUE, header=FALSE)

# question 1: Familiarity with OAP -------------------------------------------------------------

# first two rows are not answers
OAPFamiliarity<-survey[-c(1,2),10]
oap.familiar<-vector(mode="numeric",length=135)
oap.vfamiliar<-vector(mode="numeric",length=135)
oap.unfamiliar<-vector(mode="numeric",length=135)
oap.vunfamiliar<-vector(mode="numeric",length=135)
a.1<-vector(mode="numeric",length=135)

for(i in 1:length(OAPFamiliarity)){
  if(OAPFamiliarity[i]=="Familiar") oap.familiar[i]=1 else oap.familiar[i]=0
  if(OAPFamiliarity[i]=="Very familiar") oap.vfamiliar[i]=1 else oap.vfamiliar[i]=0
  if(OAPFamiliarity[i]=="Unfamiliar") oap.unfamiliar[i]=1 else oap.unfamiliar[i]=0
  if(OAPFamiliarity[i]=="Very unfamiliar") oap.vunfamiliar[i]=1 else oap.vunfamiliar[i]=0
}

oap.fam<-oap.familiar+oap.vfamiliar
oap.unfam<-oap.unfamiliar+oap.vunfamiliar

for(i in 1:length(OAPFamiliarity)){
  if(OAPFamiliarity[i]=="Familiar" | OAPFamiliarity[i]=="Very familiar") a.1[i]=1
  if(OAPFamiliarity[i]=="Unfamiliar" | OAPFamiliarity[i]=="Very unfamiliar") a.1[i]=0
}

# question 11: Familiarity with RG -------------------------------------------------------------

RGFamiliarity<-survey[-c(1,2),75]
rg.familiar<-vector(mode="numeric",length=135)
rg.vfamiliar<-vector(mode="numeric",length=135)
rg.unfamiliar<-vector(mode="numeric",length=135)
rg.vunfamiliar<-vector(mode="numeric",length=135)
a.11<-vector(mode="numeric",length=135)

for(i in 1:length(RGFamiliarity)){
  if(RGFamiliarity[i]=="Familiar") rg.familiar[i]=1 else rg.familiar[i]=0
  if(RGFamiliarity[i]=="Very familiar") rg.vfamiliar[i]=1 else rg.vfamiliar[i]=0
  if(RGFamiliarity[i]=="Unfamiliar") rg.unfamiliar[i]=1 else rg.unfamiliar[i]=0
  if(RGFamiliarity[i]=="Very unfamiliar") rg.vunfamiliar[i]=1 else rg.vunfamiliar[i]=0
}

rg.fam<-rg.familiar+rg.vfamiliar
rg.unfam<-rg.unfamiliar+rg.vunfamiliar

for(i in 1:length(RGFamiliarity)){
  if(RGFamiliarity[i]=="Familiar" | RGFamiliarity[i]=="Very familiar") a.11[i]=1
  if(RGFamiliarity[i]=="Unfamiliar" | RGFamiliarity[i]=="Very unfamiliar") a.11[i]=0
}

# questions 2, 3, 12, 13: Participation and knowledge --------------------------------------------------

oap.knowledge<-survey[-c(1,2),11:25] #matrix of question 2, which assesses knowledge of OA policy
rg.knowledge<-survey[-c(1,2),76:83] #matrix of question 12, which assesses knowledge of RG policy
oap.participation<-survey[-c(1,2),26] #vector of question 3, which confirms or denies participation in OA
rg.participation<-survey[-c(1,2),84] #vector of question 13, which confirms or denies participation in RG

#numeric matrices and vectors 
oap.know<-matrix(data=0,ncol=15,nrow=135)
rg.know<-matrix(data=0,ncol=8,nrow=135)
oap.use<-vector(mode="numeric",length=135)
rg.use<-vector(mode="numeric",length=135)

for(i in 1:dim(oap.knowledge)[1]){ #goes through ever row and then checks columns
  for(j in c(1,4,7,9,10,12)){
    if(oap.knowledge[i,j]=="") oap.know[i,j]=0 else oap.know[i,j]=1
  }
  for(j in c(2,3,5,6,8,11,13,14,15)){
    if(oap.knowledge[i,j]=="") oap.know[i,j]=1 else oap.know[i,j]=0
  }
  for(j in c(1,2)){
    if(rg.knowledge[i,j]=="") rg.know[i,j]=0 else rg.know[i,j]=1
  }
  for(j in c(3,4,5,6,7,8)){
    if(rg.knowledge[i,j]=="") rg.know[i,j]=1 else rg.know[i,j]=0
  }
  if(oap.participation[i]=="Yes") oap.use[i]=1 else oap.use[i]=0
  if(rg.participation[i]=="Yes") rg.use[i]=1 else rg.use[i]=0
}

oap.score<-rowSums(oap.know)
rg.score<-rowSums(rg.know)

# question 10: Legality of OAP -------------------------------------------------------------

OAPLegality<-survey[-c(1,2),73]
oap.legal<-vector(mode="numeric",length=135)
oap.illegal<-vector(mode="numeric",length=135)
a.10<-vector(mode="numeric",length=135)

for(i in 1:length(OAPLegality)){
  if(OAPLegality[i]=="Legal under copyright law") { oap.legal[i]=1 ; a.10[i]=1  } else oap.legal[i]=0
  if(OAPLegality[i]=="Violates the copyright of the publisher") { oap.illegal[i]=1 ; a.10[i]=0 } else oap.illegal[i]=0
}

# question 19: Legality of RG -------------------------------------------------------------

RGLegality<-survey[-c(1,2),122]
rg.legal<-vector(mode="numeric",length=135)
rg.illegal<-vector(mode="numeric",length=135)
a.19<-vector(mode="numeric",length=135)

for(i in 1:length(RGLegality)){
  if(RGLegality[i]=="Legal under copyright law") {rg.legal[i]=1;a.19[i]=1} else rg.legal[i]=0
  if(RGLegality[i]=="Violates the copyright of the publisher") {rg.illegal[i]=1;a.19[i]=0} else rg.illegal[i]=0
}

# Linear Models -------------------------------------------------------

OAP.fit<-lm(oap.use ~ oap.fam + oap.unfam + oap.score + oap.legal + oap.illegal + rg.use)
summary(OAP.fit)

RG.fit<-lm(rg.use ~ rg.fam + rg.unfam + rg.score + rg.legal + rg.illegal + oap.use)
summary(RG.fit)